<!-- 采购管理-采购任务列表 -->
<template>
  <pageHeader title="采购订单详情页" :breadCrumb="breadCrumb" :show-back="true">
  </pageHeader>
  <div class="title">
    <h1>采购订单</h1>
  </div>
  <div style="display: flex">
    <div class="form-item">
      <div class="title-item">
        <a-space style="margin-left: 40px">
          <div
            style="
              width: 5px;
              height: 23px;
              background-color: rgba(61, 126, 255, 1);
              border: none;
              border-radius: 2px;
            "
          ></div>
          <div>基本信息</div></a-space
        >
      </div>
      <div class="demand-index">
        <a-form
          ref="form"
          :model="demand.info"
          autoLabelWidth
          :layout="(layout as Tlayout)"
        >
          <a-row :gutter="16">
            <a-col
              v-for="item in demandForm?.info?.design?.form"
              :key="item.id"
              :span="item.span"
            >
              <FormItem
                :name="item.name"
                :componentType="item.componentType"
                v-model:props="item.props"
                :required="item.required"
                :disabled="item.disabled"
                v-model:values="item.values"
              />
            </a-col>
          </a-row>
        </a-form>
      </div>
    </div>
  </div>

  <a-modal v-model:visible="visible" @ok="handleOk" @cancel="handleCancel">
    <template #title> 审核信息 </template>
    <div
      ><a-form :model="demand" auto-label-width>
        <a-form-item field="priority" label="优先级" required>
          <a-radio-group v-model="demand.info.priority">
            <a-radio :value="4">紧急</a-radio>
            <a-radio :value="3">高</a-radio>
            <a-radio :value="2">中</a-radio>
            <a-radio :value="1">低</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form></div
    >
  </a-modal>
</template>

<script lang="ts" setup>
  import { reactive, ref, computed, onMounted } from 'vue';
  import { useRouter, useRoute } from 'vue-router';
  import dayjs from 'dayjs';
  import {
    // postPurchaseApply,
    getSinglePurchaseOrder,
    putSinglePurchaseOrder,
  } from '@/api/buy-management/buy-order/index';
  import { getOpenTemp } from '@/api/buy-management/get-template/index';

  const router = useRouter();
  const route = useRoute();
  const breadCrumb: string[] = ['采购管理', '采购管理', '采购订单详情页'];
  const layout = ref('vertical');
  type Tlayout = 'inline' | 'horizontal' | 'vertical' | undefined;
  interface DemandType {
    info: {
      number: string; //采购订单单号
      name: string; //采购订单名称
      description: string; //秒速
      templateId: number; //模板id
      contractId: number; //合同编号
      supplierId: number; //供应商id
      priority: number; //优先级
      state: number; //状态
      salesman: number; //业务员
      totalPrice: number; //总金额
      product: any; //订单产品
      deliveryItem: any; //交货要求
      extend: string;
      isAutoToOrder: number;
      extends: any;
      file: any;
    };
  }
  const demand = reactive<DemandType>({
    info: {
      number: '',
      name: '',
      description: '',
      templateId: 0,
      contractId: 0,
      supplierId: 0,
      priority: 0,
      state: 0,
      salesman: 0,
      totalPrice: 0,
      extend: '',
      isAutoToOrder: 0,
      product: [],
      deliveryItem: [],
      userStaffId: null,
      extends: null,
      file: [],
    },
  });
  interface DemandFormType {
    info: {
      id: number;
      flowId: number;
      design: {
        form: any[];
      };
    };
  }

  const keys = Object.keys(demand.info);
  const msgKays = ref();
  const demandForm = reactive<DemandFormType>({
    info: { id: 0, flowId: 0, design: { form: [] } },
  });

  let id = Number(route.query.id);
  let isOrderType = ref(false);

  const getTemp = async () => {
    const res = await Promise.all([
      getOpenTemp({ type: 3 }),
      getSinglePurchaseOrder({ id }),
    ]);
    demand.info = res[1];
    if (!Array.isArray(demand.info.extends)) {
      isOrderType.value = true;
      demandForm.info = res[0];
      msgKays.value = Object.keys(demand.info.extends);
    } else {
      isOrderType.value = false;
      demandForm.info.design.form = res[1].extends;
    }
    demandForm.info.design.form.sort((a: any, b: any) => a.sort - b.sort);
    demandForm.info.design.form.forEach((item: any) => {
      item.disabled = true;
      if (item.values !== '') {
        // item.values = JSON.parse(item.values);
        if (item.values[0] === '') {
          item.values = [];
        }
      }

      if (isOrderType.value) {
        if (item.fieldName && msgKays.value.includes(item.fieldName)) {
          item.values = [];
          item.values[0] = demand.info.extends[item.fieldName];
        }
      }

      if (item.code === 'BUY-DETAILED') {
        item.props.data = demand.info.product;
      }
      if (item.code === 'FILE') {
        item.props.data = demand.info.file;
      }
    });
  };
  getTemp();

  const visible = ref(false);

  // const handleClick = () => {
  //   visible.value = true;
  // };

  // const handleOk = async () => {
  //   visible.value = false;
  //   demandForm.info.design.form.forEach((item) => {
  //     if (item.fieldName && keys.includes(item.fieldName)) {
  //       demand.info[item.fieldName] = item.values[0];
  //     }
  //   });
  //   demand.info.applyTime = new Date().toJSON();
  //   demand.info.deliveryTime = new Date().toJSON();
  //   demand.info.state = 2;
  //   await postPurchaseApply(demand.info);
  // };
  // const handleCancel = () => {
  //   visible.value = false;
  // };

  const save = async () => {
    demandForm.info.design.form.forEach((item) => {
      if (item.fieldName && keys.includes(item.fieldName)) {
        demand.info[item.fieldName] = item.values[0];
      }
    });
    demand.info.extend = JSON.stringify(demandForm.info.design.form);
    await putSinglePurchaseOrder({ ...demand.info });
    router.back();
  };
</script>

<style scoped lang="less">
  // .content-head {
  // }
  .title {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .title-item {
    width: 95%;
    height: 60px;
    margin: auto;
    background-color: rgba(245, 247, 250);
    line-height: 60px;
    margin-top: 20px;
  }
  .demand-index {
    padding: 25px 140px;
  }

  .approve-item {
    margin: 25px auto;
    width: 35%;
    background-color: rgba(255, 255, 255, 1);
    border-radius: 8px;
    box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.349019607843137);
    height: 600px;
  }
  .form-item {
    width: 100%;
  }
</style>
